import ddApi from "@/components/js/ddApi";
import service from "@/api";

// 配置
const dingtalk = {
  // corpId: 'dingb9b4f71ed6f8ff6d35c2f4657eb6378f', //云访客改成从链接上截取
  jsApiList: ['biz.contact.complexPicker'] //要签名的jsApiList
}


// 免登
async function authLogin() {

  const getUrlParamsRes = JSON.parse(localStorage.getItem('getUrlParamsRes'));

  // 获取临时授权码
  const requestAuthCodeRes = await ddApi.requestAuthCode(getUrlParamsRes.corpId);
  if (!requestAuthCodeRes.success) {
    return new Promise((resolve) => {
      resolve(requestAuthCodeRes);
    })
  }

  // 免登掉接口
  const getUserInfoRes = await service.getUserInfo({
    code: requestAuthCodeRes.data.code
  });
  if (!getUserInfoRes.success) {
    return new Promise((resolve) => {
      resolve(getUserInfoRes);
    })
  }

  // 设置请求头
  const setAuthorizationRes = await service.setAuthorization(getUserInfoRes.data.token);
  if (!setAuthorizationRes.success) {
    return new Promise((resolve) => {
      resolve(setAuthorizationRes);
    })
  }
  // 返回免登的结果
  return new Promise((resolve) => {
    resolve(getUserInfoRes);
  })
}


async function ddConfig(params) {
  const getJsConfigRes = await service.getJsConfig();
  if (!getJsConfigRes.success) {
    return new Promise((resolve) => {
      resolve(getJsConfigRes);
    })
  }
  let jsConfig = getJsConfigRes.data;
  jsConfig.jsApiList = dingtalk.jsApiList;
  const ddConfigRes = await ddApi.ddConfig(jsConfig);
  // 返回签名的结果
  return new Promise((resolve) => {
    resolve(getJsConfigRes);
  })
}





export {
  authLogin,
  ddConfig
}
